package com.qing.controller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.qing.pojo.User;
import com.qing.util.JsonUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController//=responseBody+Controller
public class UserController {

    /**
     * jackson
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping(value = "/j1")
//    @ResponseBody   //不会走视图解析器，会直接返回一个字符串
    public String json1() throws JsonProcessingException {

        ObjectMapper mapper = new ObjectMapper();

        User user = new User("张三",1,"男");

        String str = mapper.writeValueAsString(user);

        return str;
    }

    @RequestMapping("/j2")
    public String json2() throws JsonProcessingException {
//        ObjectMapper mapper = new ObjectMapper();

        List<User> userArrayList = new ArrayList<>();
        User user1 = new User("张三",1,"男");
        User user2 = new User("李四",1,"男");
        User user3 = new User("王五",1,"男");
        User user4 = new User("赵六",1,"男");

        userArrayList.add(user1);
        userArrayList.add(user2);
        userArrayList.add(user3);
        userArrayList.add(user4);

//        String str = mapper.writeValueAsString(userArrayList);

        return JsonUtil.getJson(userArrayList);
    }

    @RequestMapping("/j3")
    public String json3() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();

        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        mapper.setDateFormat(simpleDateFormat);

        Date date = new Date();

//        String format = simpleDateFormat.format(date);

        String str = mapper.writeValueAsString(date);

        return str;
    }

//  util版
    @RequestMapping("/j4")
    public String json4() throws JsonProcessingException {
        Date date = new Date();
//        String format = simpleDateFormat.format(date);
        return JsonUtil.getJson(date,"yyyy-MM-dd HH:mm:ss");
    }


    /**
     * fastjson
     */
    @RequestMapping("/j5")
    public String json5(){
        List<User> userArrayList = new ArrayList<>();
        User user1 = new User("张三",1,"男");
        User user2 = new User("李四",1,"男");
        User user3 = new User("王五",1,"男");
        User user4 = new User("赵六",1,"男");

        userArrayList.add(user1);
        userArrayList.add(user2);
        userArrayList.add(user3);
        userArrayList.add(user4);

        String str = JSON.toJSONString(userArrayList);
        return str;
    }
}
